package com.yunhe.common.util;

import com.yunhe.common.client.WebLogClient;
import com.yunhe.common.constant.LogConsts;
import com.yunhe.common.constant.SystemConsts;
import com.yunhe.common.model.authority.Station;
import com.yunhe.common.model.authority.User;
import com.yunhe.common.model.data.SystemMonitorLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Time;
import java.util.Date;

/**
 * 日志工具类
 * @author liuronglei
 */

@Component
public class LogUtil {
    @Autowired
    private WebLogClient webLogClient;
    @Autowired
    private HeaderUtil headerUtil;

    /**
     * 新建日志
     * @param log 日志对象
     */
    public void createLog(SystemMonitorLog log) {
        webLogClient.createSystemLog(log);
    }

    /**
     * 新建日志
     * @param stationCode 电站代码
     * @param typeCode 类型代码
     * @param content 内容
     * @param type 类型文字
     * @param date 日期
     */
    public void createLog(Long stationCode, Long typeCode, String content, String type, Date date) {
        SystemMonitorLog log = new SystemMonitorLog();
        log.setStationId(stationCode);
        log.setTypeId(typeCode);
        log.setType(type);
        log.setContent(content);
        log.setDate(date);
        log.setTime(new Time(date.getTime()));
        createLog(log);
    }

    /**
     * 新建日志
     * @param stationCode 电站代码
     * @param typeCode 类型代码
     * @param content 内容
     */
    public void createLog(long stationCode, LogConsts.LOG_TYPE_CODE typeCode, String content) {
        createLog(stationCode, typeCode.value(), content, typeCode.title(), new Date());
    }

    /**
     * 新建日志
     * @param station 电站对象
     * @param typeCode 类型代码
     * @param content 内容
     */
    public void createLog(Station station, LogConsts.LOG_TYPE_CODE typeCode, String content) {
        Long stationCode = null;
        if (station != null) {
            stationCode = station.getCode();
        }
        createLog(stationCode, typeCode.value(), content, typeCode.title(), new Date());
    }

    /**
     * 新建日志
     * @param typeCode 类型代码
     * @param content 内容
     */
    public void createLog(LogConsts.LOG_TYPE_CODE typeCode, String content) {
        Station station = headerUtil.getHeaderStation();
        createLog(station, typeCode, content);
    }

    /**
     * 新建日志
     * @param typeCode 类型代码
     * @param menu 菜单名称
     * @param actionType 操作类型
     */
    public void createLog(LogConsts.LOG_TYPE_CODE typeCode, String menu, SystemConsts.ACTION actionType) {
        Station station = headerUtil.getHeaderStation();
        User user = headerUtil.getHeaderUser();
        String content = "";
        if (user != null) {
            content = user.getTitle() + "在" + menu + "页面进行了" + actionType.title() + "操作";
        }
        createLog(station, typeCode, content);
    }

    /**
     * 新建日志
     * @param typeCode 类型代码
     * @param menu 菜单名称
     * @param action 操作
     */
    public void createLog(LogConsts.LOG_TYPE_CODE typeCode, String menu, String action) {
        Station station = headerUtil.getHeaderStation();
        User user = headerUtil.getHeaderUser();
        String content = "";
        if (user != null) {
            content = user.getTitle() + "在" + menu + "页面进行了" + action + "操作";
        }
        createLog(station, typeCode, content);
    }

    /**
     * 新建日志
     * @param menu 菜单名称
     * @param actionType 操作类型
     */
    public void createActionLog(String menu, SystemConsts.ACTION actionType) {
        String action = actionType.title();
        createActionLog(menu, action);
    }

    /**
     * 新建日志
     * @param menu 菜单名称
     * @param action 操作
     */
    public void createActionLog(String menu, String action) {
        Station station = headerUtil.getHeaderStation();
        User user = headerUtil.getHeaderUser();
        createActionLog(station, user, menu, action);
    }

    /**
     * 新建日志
     * @param station 电站对象
     * @param user 用户对象
     * @param menu 菜单名称
     * @param action 操作
     */
    public void createActionLog(Station station, User user, String menu, String action) {
        String content = "";
        if (user != null) {
            content = user.getTitle() + "在" + menu + "页面进行了" + action + "操作";
        }
        createLog(station, LogConsts.LOG_TYPE_CODE.ACTION, content);
    }
}
